//
//  ZQZZPage.m
//  jianzhik
//
//  Created by huami on 2018/9/27.
//  Copyright © 2018年 WKWebView. All rights reserved.
//
#import "XXTextView.h"
#import "UIBarButtonItem+item.h"
#import "ZQZZPage.h"
#import "ZQAdressModels.h"
#import <MJExtension/MJExtension.h>
#import "ZQAFHelper.h"
#import "BRStringPickerView.h"
#import "ZQDataManger.h"
#import "UIView+AddClickedEvent.h"
#import "ZQQrcodeGenerationPage.h"
#import "ZZModel.h"
#import "ErWeiMaPage.h"
@interface ZQZZPage ()<UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *myAddressFromLabel;

@property (weak, nonatomic) IBOutlet XXTextView *myAddressToTextView;
@property (weak, nonatomic) IBOutlet UITextField *acountCountTextField;

@property (weak, nonatomic) IBOutlet UILabel *bzType;
/** listArr */
@property (nonatomic, strong) NSMutableArray <ZQAdressModels *> *listMuArr;
@property (nonatomic, strong) NSMutableArray *bzTypelistMuArr;

@end

@implementation ZQZZPage

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNavBar];
    self.navigationController.delegate =self;
    self.myAddressToTextView.xx_placeholder = @"在此粘贴目的地址";
    [self.bzType addClickedBlock:^(id obj) {
        NSString *defaultStr = self.bzType.text.length > 0 ? self.bzType.text : @"";
        [BRStringPickerView showStringPickerWithTitle:@"币种" dataSource:self.bzTypelistMuArr defaultSelValue:defaultStr isAutoSelect:YES themeColor:kTinColor resultBlock:^(id selectValue) {
            //        textField.text = self.infoModel.educationStr = selectValue;
            [self.listMuArr enumerateObjectsUsingBlock:^(ZQAdressModels * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([obj.Symbol isEqualToString:selectValue]) {
                    self.bzType.text = selectValue;
                    self.myAddressFromLabel.text = obj.Address;
                }
            }];
            
        } cancelBlock:^{
            NSLog(@"点击了背景视图或取消按钮");
        }];
    }];
    [self.footButton setTitle:@"生成待签名的二维码" forState:UIControlStateNormal];
    [self.footButton addTarget:self action:@selector(generateQrcode) forControlEvents:UIControlEventTouchUpInside];

    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [ZQAFHelper afnetworkRequestWithGetURL:@"/v1/active/listaddressactive" parameters:parameters view:self.view finishedBlcok:^(NSDictionary *dict) {
        if (self.listMuArr.count > 0) {
            [self.listMuArr removeAllObjects];
        }
        if (self.bzTypelistMuArr.count > 0) {
            [self.bzTypelistMuArr removeAllObjects];
        }
        self.listMuArr = [ZQAdressModels mj_objectArrayWithKeyValuesArray:dict[@"Data"]];
        
        if (self.listMuArr.count == 0) {
            [UIHelper showTipsWithText:@"查无地址数据" afterHideHud:2 inView:self.view completion:^{
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }else {
            [self.listMuArr enumerateObjectsUsingBlock:^(ZQAdressModels * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [self.bzTypelistMuArr addObject:obj.Symbol];
            }];
            ZQAdressModels *model = self.listMuArr[0];
            self.bzType.text = model.Symbol;
            self.myAddressFromLabel.text = model.Address;
            [UIHelper dismissLoadingInView:self.view];
        }
    }];
}

- (void)setUpNavBar
{
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem backItemWithImage:@"扫描" hightImage:@"扫描" target:self action:@selector(qrcodeClick) title:nil];
    // titleview search_big searchWorkInfo
    //    self.navigationItem.title =  @"首页";
}

- (void)qrcodeClick
{
    ErWeiMaPage *vc = [ErWeiMaPage new];
    vc.flag = @"rightItem";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)generateQrcode
{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"Symbol"] = self.bzType.text;
    parameters[@"From"] = self.myAddressFromLabel.text;
    parameters[@"To"] = self.myAddressToTextView.text;
    parameters[@"Amount"] = self.acountCountTextField.text;

    [ZQAFHelper afnetworkRequestWithPostURL:@"/v1/active/getunsigne" parameters:parameters view:self.view finishedBlcok:^(NSDictionary *dict) {
        
        ZZModel *model = [ZZModel new];
        model.bzType = self.bzType.text;
        model.from = self.myAddressFromLabel.text;
        model.to = self.myAddressToTextView.text;
        model.count = [self.acountCountTextField.text doubleValue];
        int code =   [dict[@"Code"] intValue];;
        if (code == 0) {
            model.Unsigne = dict[@"Data"][@"Unsigne"];
            ZQQrcodeGenerationPage *vc = [ZQQrcodeGenerationPage new];
            vc.zzModel = model;
            vc.Unsigne = dict[@"Data"][@"Unsigne"];
            vc.Flag = @"ZZ";
            [self.navigationController pushViewController:vc animated:YES];
        }
       [UIHelper dismissLoadingInView:self.view];
    }];
}

- (NSMutableArray<ZQAdressModels *> *)listMuArr
{
    if (!_listMuArr) {
        _listMuArr = [[NSMutableArray alloc] init];
    }
    return _listMuArr;
}

- (NSMutableArray *)bzTypelistMuArr
{
    if (!_bzTypelistMuArr) {
        _bzTypelistMuArr = [[NSMutableArray alloc] init];
    }
    return _bzTypelistMuArr;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.bzTypelistMuArr removeAllObjects];
}
@end
